package ezonsport.android.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

import ezonsport.android.R;
import ezonsport.android.data.DataHelper;
import ezonsport.android.ui.userRegister_Login.UserRegisterController;

public class RegisterActivity extends OrmLiteBaseActivity<DataHelper> {
	
	private Button mBtnRegister;
	private Button mBtnCancel;
	
	//Register Informations Edit
	private EditText mEmailEdit;
	private EditText mPwdEdit;
	private EditText mPwdConfirmEdit;	
	private TextView mProgressText;
	
	//Register informations content
	private String mEmailText;
	private String mPwdText;
	private String mPwdConfirmText;
	
	private UserRegisterController mController;
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.register);
        
        mBtnRegister = (Button)findViewById(R.id.btn_register);
        mBtnCancel = (Button)findViewById(R.id.btn_cancel);
        
        mEmailEdit = (EditText)findViewById(R.id.register_email);
        mPwdEdit = (EditText)findViewById(R.id.register_userpassword);
        mPwdConfirmEdit = (EditText)findViewById(R.id.register_passwordagain);
        mProgressText = (TextView)findViewById(R.id.register_progress);
        
        mController = new UserRegisterController(getHelper());
        
        mBtnRegister.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mEmailText = mEmailEdit.getText().toString();
				mPwdText = mPwdEdit.getText().toString();
				mPwdConfirmText = mPwdConfirmEdit.getText().toString();
				if(TextUtils.isEmpty(mEmailText)){
					updateProgress(getString(R.string.register_status_null_email));
				}else if(TextUtils.isEmpty(mPwdText) || TextUtils.isEmpty(mPwdConfirmText)){
					updateProgress(getString(R.string.register_status_null_pwd));
				}else if(!mPwdText.equals(mPwdConfirmText)){
					updateProgress(getString(R.string.register_status_pwd_not_consistent));
				}
				
				String msg = mController.Register(mEmailText, mPwdText, mPwdConfirmText);
				if(msg == null){
					updateProgress(getString(R.string.register_status_success));
					Intent intent = new Intent();
					intent.setClass(RegisterActivity.this,LoginActivity.class);
					startActivity(intent);	
				}else{//register failed
				}
					
			}
        });
        
        mBtnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(RegisterActivity.this,LoginActivity.class);
				startActivity(intent);	
			}
        });
	}
	 private void updateProgress(String progress) {
			mProgressText.setText(progress);
		} 
}
